import {SocketModul,UploadFile} from '@/common/config.js'
export default {
	data(){
		return{
			_imgArray:[],//上传图片数组
			uploadIndex:0,//上传数标
			isUpload:false,
		}
	},
	computed:{
		isSocketOpen(){
			return this.$store.state.chat.isSocketOpen
		},
		ischatOnline(){
			return this.$store.state.chat.isOnline
		},
		user_info(){
			return this.$store.state.chat.user_info
		},
	},
	onLoad() {
		
	},
	onShow(){
		if(this.isSocketOpen && !this.ischatOnline){
			this.init()
		}
	},
	watch:{
		'isSocketOpen':function(value){
			if(value){
				setTimeout(()=>{
					if (this.isSocketOpen && !this.ischatOnline){
						this.init()
					}
				},50)
			}
		},
		'ischatOnline':function(value){
			if(value){
				if(this.pageInit){
					setTimeout(()=>this.pageInit(),50)
				}
			}
		}
	},
	methods:{
		init(){
			this.initUserInfo().then(()=>{
				if(!this.ischatOnline){
					this.loginChat()
				}
			})
		},
		//初始化用户信息
		initUserInfo(){
			let self=this
			return new Promise((resolve, reject)=>{
				if(self.user_info.id){
					resolve()
				}else{
					uni.getStorage({
						key:'chat_user_info',
						success:function(response){
							if (response.data.id && response.data.token ) {
								let {id,nickname,figureurl}=response.data
								self.$store.commit('setChatUserInfo',{id,nickname,figureurl})
								resolve()
							}
						},
						fail:function(){
							uni.reLaunch({url:'/pages/login/index'});//跳转到用户页面
						}
					})
				}
			})			
		},
		//登录聊天
		loginChat(){
			let self=this
			return new Promise((resolve, reject)=>{
				const value = uni.getStorage({
					key:'chat_user_info',
					success:function(response){
						let {token,exp}=response.data,date=new Date()
						if(date.getTime()>exp*1000){
							//做续期处理 待完成
						}
						let tpl={
							action:'onOpen',
							data:{user_info:self.user_info,token,exp}
						};
						self.sendSocketMessage(tpl).then(()=>{
							resolve()
						})
					},fail:function(){
						uni.reLaunch({url:'/pages/login/index'})
						reject()
					}
				});				
			})
		},
		//取得用户信息列表
		getUserList(){
			let tpl={
				action:'getUserList',
				data:{}
			};
			this.sendSocketMessage(tpl)
		},
		//发送消息
		sendSocketMessage(str){
			let module= SocketModul ,self=this
			return new Promise((resolve, reject)=>{
				if(!str || !self.isSocketOpen){
					reject()
				}else{
					if(typeof str !== 'string'){
						let {action,data}=str
						let tpl={module,action,data};
						str=JSON.stringify(tpl)
					}
					uni.sendSocketMessage({
						data: str,
						success:function(response){resolve()},
						fail:function(){
							self.$store.commit('changeSocket',false)
							reject()
						}
					});
				}
			})
		},
		//上传文件
		uploadFile(tempFilePath,data=null,tyep='img'){
			return new Promise((resolve, reject)=>{
				uni.uploadFile({
					url: UploadFile+"?type="+tyep, 
					filePath: tempFilePath,
					name: 'file',
					formData: {
							'user': 'test'// 用户鉴权信息待完成
					},
					success: function(response) {
						let res=response.data,obj=data?{...data}:{}
						if (typeof res ==='string' ){
							res =JSON.parse(res)
						}
						if(res.url){
							obj.url=res.url
							resolve(obj)
						}else{
							reject({message:'上传失败'})
						}
					},
					fail:function(err){
						reject({message:err.errMsg})
					}
				});
			})
		},
		// 发送图片信息
		sendImage(type){
			this.hideDrawer();
			this.getImage(type).then(data=>{
				this.uploadFile(data.path,data).then(response=>{
					let {url,width,height}=response
					if(url){
						let tpl={
							action:"sendToUid",
							data:{
								send_id:this.user_info.id,
								receive_id:this.receive_id,
								message_type:'img',
								url,width,height,
								time:(new Date() ).getTime()
							}
						};
						if(this.user_info.id != this.receive_id){
							this.$store.commit('receiveChatPrivate',tpl.data)
						}
						this.sendSocketMessage(tpl)
					}
				}).catch(err=>{
					this.popup(err)
				})
			}).catch(err=>{
				this.popup(err)
			});
		},
		//  取得图片信息
		getImage(type){
			return new Promise((resolve, reject)=>{
				uni.chooseImage({
					sourceType:[type],
					count:1,
					success:function (res){
						let tempFile=res.tempFiles[0]
						if(tempFile.size > 1024*100){
							reject({message:'图片不能大于100K'})
						}
						uni.getImageInfo({
							src: tempFile.path,
							success: function (image){
								let {width,height,path}=image
								resolve({width,height,path,size:tempFile.size})
							}
						});
					}
				});
			})
		},
		//错误信息
		popup(err){
			this.popupMessage=err.message||'末知错误'
			this.showPopup=true
			setTimeout(()=>this.showPopup=false,1000)
		},
	}
}